define(['jquery', 'bootstrap', 'backend', 'table', 'form','fileinput','vue'], function ($, undefined, Backend, Table, Form,fileinput,Vue) {
//初始化fileinput控件（第一次初始化）
    var file_path = '';
    function initFileInput(ctrlName, uploadUrl) {
        $('#upload_file').fileinput('refresh',{
            language: 'zh', //设置语言
            uploadUrl: uploadUrl, //上传的地址
            allowedFileExtensions : ['xls','xlsx'],//接收的文件后缀
            showUpload: true, //是否显示上传按钮
            showRemove:false,
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            initialPreviewConfig:[],
            previewFileIcon: "<i  class='glyphicon glyphicon-king'></i>",
        }).on("fileuploaded", function(event, data) {
            console.info(ctrlName);
            console.info(data);
            if(data.response.code == 1){
                Toastr.success(data.response.msg);
                $('#'+ctrlName).val(data.response.data.url);
                file_path = data.response.data.url;
            }else{
                Toastr.error(data.response.msg);
            }
        }).on("filesuccessremove", function (event, data, previewId, index) {
            if(file_path !=''){
                $.post("adminpublic/ajax_delete_file", {
                    file_path : file_path
                }, function (res) {
                    if(data.response.code == 1){
                        Toastr.success(data.response.msg);
                    }else{
                        Toastr.error(data.response.msg);
                    }
                });
            }
            return true;
        });
    }
    function initSaveVue() {
        var vue = new Vue({
            el:'#form_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                village_list: [],
                province_id : '',
                city_id : '',
                county_id : '',
                village_id : '',
                building_number : '',
                floor_number : '',
                room_number : '',
                water_charge : '',
                property_charge : '',
                detail : '',
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                    this.village_list = [];
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                    this.village_list = [];
                },
                county_id:function (val) {
                    this.county_id = val;
                    this.getVillageList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                },
                getVillageList:function(){
                    var that = this;
                    $.get("adminpublic/ajax_village_list", {
                        county_id : that.county_id
                    }, function (res) {
                        that.village_list = res;
                    });
                }
            }
        });
        return vue;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'owner/index',
                    add_url: 'owner/add',
                    edit_url: 'owner/edit',
                    del_url: 'owner/del',
                    multi_url: 'owner/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'id_card', title: __('ID card'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('House property list'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-topic-list btn-dialog',
                                url:'houseproperty/index'
                            }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initSaveVue();
            vue.getProvinceList();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        import:function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = new Vue({
                el:'#index_div',
                data:{
                    province_list: [],
                    city_list: [],
                    county_list: [],
                    village_list: [],
                    province_id : '',
                    city_id : '',
                    county_id : '',
                    village_id : '',
                },
                watch:{
                    province_id:function (val) {
                        this.province_id = val;
                        this.getCityList();
                        this.village_list = [];
                        this.city_list = [];
                        this.county_list = [];
                    },
                    city_id:function (val) {
                        this.city_id = val;
                        this.getCountyList();
                        this.village_list = [];
                        this.county_list = [];
                    },
                    county_id:function (val) {
                        this.county_id = val;
                        this.getVillageList();
                    }
                },
                methods: {
                    getProvinceList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : 0
                        }, function (res) {
                            that.province_list = res;
                        });
                    },
                    getCityList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : that.province_id
                        }, function (res) {
                            that.city_list = res;
                        });
                    },
                    getCountyList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : that.city_id
                        }, function (res) {
                            that.county_list = res;
                        });
                    },
                    getVillageList:function(){
                        var that = this;
                        $.get("adminpublic/ajax_village_list", {
                            county_id : that.county_id
                        }, function (res) {
                            that.village_list = res;
                        });
                    }
                }
            });
            vue.getProvinceList();
            var url = Config.site.cdnurl+'/index.php/admin/adminpublic/upload';
            var controlName = 'upload_file_path';
            initFileInput(controlName,url);
        }
    };
    return Controller;
});